let list = [
    {
        id: 1,
        user_id: 1,
        role_id: 1,
        update_at: null,
        is_del: 0,
        role_name: '超级管理员'
    },
    {
        id: 2,
        user_id: 1,
        role_id: 2,
        update_at: null,
        is_del: 0,
        role_name: '用户'
    },
    {
        id: 5,
        user_id: 2,
        role_id: 2,
        update_at: null,
        is_del: 0,
        role_name: '用户'
    }
]

let map = new Map();
for (let item of list) {
    let current = map.get(item.user_id);
    if (current) {
        current.roles.push({ role_id: item.role_id, role_name: item.role_name })
    }
    else {
        map.set(item.user_id, {
            user_id: item.user_id,
            roles: [{ role_id: item.role_id, role_name: item.role_name }]
        })
    }
}

console.log(JSON.stringify(Array.from(map.values())));




